package com.pet.json.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;



public class BeanToJson {
	public static JSONObject beanTojson(Object bean) {
		JSONObject o = new JSONObject();
		PropertyDescriptor[] props = null;
	     try {
	       props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();
	     } catch (IntrospectionException e) {
	      e.printStackTrace();
	     }
	     if (props != null) {
	    	 for (int i = 0; i < props.length; i++) {
	    		 //if()
	    		 
	    		 try {
		    		 String name = props[i].getName();
		    		 Object obj = props[i].getReadMethod().invoke(bean);
	    		 }catch(Exception e){
	    			 
	    		 }
	    	 }
	     }
		
		return o;
	}
	private static String objectTojson(Object obj) {
	     if (obj == null) {
	    	 return "";
	     } else if (obj instanceof String ||
	         obj instanceof Integer ||
	         obj instanceof Float ||
	         obj instanceof Boolean ||
	         obj instanceof Short ||
	         obj instanceof Double ||
	         obj instanceof Long ||
	         obj instanceof BigDecimal ||
	         obj instanceof BigInteger ||
	         obj instanceof Byte) {
	        return dealNull(obj);
	     } else if (obj instanceof Object[]) {

	     } else if (obj instanceof List) {
	       
	     } else if (obj instanceof Map) {
	    	 
	     } else if (obj instanceof Set) {
	    	 
	     } else if (obj instanceof Date){
	    	 
	     }else{
	    	 
	     }
	     return null;
	   }
	/***/
	public static String arrayTojson(Object[] array) {
	     StringBuilder json = new StringBuilder();
	     json.append("[");
	     if (array != null && array.length > 0) {
	       for (Object obj : array) {
	         //json.append(object2json(obj));
	         json.append(",");
	       }
	       json.setCharAt(json.length() - 1, ']');
	     } else {
	       json.append("]");
	     }
	     return json.toString();
	   }
	public static String dealNull(Object obj) {
		String str = "";
		if(obj!=null){
			if(obj instanceof String){
				str = (String)obj;
			}else{
				str = obj.toString();
			}
		}
		return str;
	}
}
